//
//  ViewController.m
//  MumicDemo
//
//  Created by zhiyou on 16-5-26.
//  Copyright (c) 2016年 孙健. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController () {
    NSTimer *_timer;
}

@property (nonatomic, strong) AVAudioPlayer *play;

@property (weak, nonatomic) IBOutlet UISlider *slide;
@end

@implementation ViewController

//- (AVAudioPlayer *)play {
//    if (!_play) {
//        _play = [[AVAudioPlayer alloc] initWithContentsOfURL:<#(NSURL *)#> error:nil];
//    }
//    return _play; 
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatPlayer:@"雨一直下.mp3"];


    
}

- (void)creatPlayer:(NSString *)url {
    if (self.play) {
        self.play = nil;
    }
    NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:url ofType:nil]];
    self.play = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];

    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [self playSound :@"paipai.caf"];
}

void soundCompleteCallback(SystemSoundID soundID,void * clientData){
    NSLog(@"播放完成...");
}

// 音频

/**
 *
 1 调用AudioServicesCreateSystemSoundID(   CFURLRef  inFileURL, SystemSoundID*   outSystemSoundID)函数获得系统声音ID。
 2 如果需要监听播放完成操作，则使用AudioServicesAddSystemSoundCompletion(  SystemSoundID inSystemSoundID,
 CFRunLoopRef  inRunLoop, CFStringRef  inRunLoopMode, AudioServicesSystemSoundCompletionProc  inCompletionRoutine, void*  inClientData)方法注册回调函数。
 3 调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效（后者带有震动效果）。
 */

- (void)playSound:(NSString *)name {
    /**
     *  Foundation & CoreFoundation
        折两个对象不能直接转换,需要用到桥接__bridge
        参数1 : 文件的URL
        参数2:声音ID Card
     */
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    //NSURL *fileUrl = [NSBundle mainBundle] URLForResource:<#(NSString *)#> withExtension:<#(NSString *)#>
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    
    /**
     *  2
        参数4 当音频播放完后回调用这份方法
     *
     */
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    /**
     *  3 播放音频
     */
    AudioServicesPlaySystemSound(soundID);
}

// 音乐:歌曲...MP3
- (void) playMusic:(NSString *)name {
    
    
}
- (IBAction)play:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        // 开始
        [self.play play];
       
        [self startTimer];
        _timer.fireDate = [NSDate distantPast];//恢复定时器

    } else {
        // 关闭
        [self.play pause];
        // 改动
        _timer.fireDate = [NSDate distantFuture];//暂停定时器，注意不能调用invalidate方法，此方法会取消，之后无法恢复
    }
    
}

- (IBAction)upClick:(UIButton *)sender {
    [self creatPlayer:@"A.mp3"];
}

- (IBAction)downClick:(UIButton *)sender {
    
    [self creatPlayer:@"C.mp3"];
}

- (void)viewDidDisappear:(BOOL)animated {
    [self stopTimer];
    
}

- (void)startTimer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTime) userInfo:nil repeats:YES];
    }
}

- (void)onTime {
    // 速率
    //self.play.rate = 2;
    NSLog(@"播放总时间 :%f",self.play.duration);
    
    NSLog(@"当前播放时间 : %f",self.play.currentTime);
    
    float progress = self.play.currentTime /self.play.duration;
    self.slide.value = progress;
    
    
}

- (void)stopTimer {
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

// 当前位置 = 总时长 *比例
// 拖动一卡一卡,重写touchBegain方法
- (IBAction)sliderChange:(id)sender {

    self.play.currentTime = self.play.duration * self.slide.value;
    
}



@end
